package com.qys.livesMall.shop.controller.sys;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.app.user.service.IH2AppUserService;
import com.qys.livesMall.cache.words.annotation.CheckWord;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.shop.modal.dto.OrderCommentSysDto;
import com.qys.livesMall.shop.modal.dto.OrderCommentUserDto;
import com.qys.livesMall.shop.modal.dto.ShopProductDto;
import com.qys.livesMall.shop.modal.entity.*;
import com.qys.livesMall.shop.modal.query.QueryGoodsComment;
import com.qys.livesMall.shop.modal.query.QueryOrderComment;
import com.qys.livesMall.shop.modal.vo.OrderCommentVo;
import com.qys.livesMall.shop.service.ShopGoodsInfoService;
import com.qys.livesMall.shop.service.ShopGoodsSkuService;
import com.qys.livesMall.shop.service.ShopOrderCommentService;
import com.qys.livesMall.shop.service.ShopOrderSkuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品评论表 前端控制器
 * </p>
 *
 * @author gmx
 * @since 2023-06-30
 */
@RestController
@RequestMapping("/sys/shopOrderComment")
@Tag(name = "app-商品评论",description = "app-商品评论")
public class SysShopOrderCommentController {
    @Resource
    private IH2AppUserService userService;
    @Resource
    private ShopOrderCommentService commentService;
    @Resource
    private ShopOrderSkuService skuService;
    @Resource
    private ShopGoodsSkuService goodsSkuService;
    @Resource
    private ShopGoodsInfoService goodsInfoService;
    @Operation(summary = "添加评论模仿用户")
    @PostMapping("/useradd")
    @CheckWord
    public R useradd(@RequestBody OrderCommentUserDto commentDto){
        ShopOrderComment orderComment = new ShopOrderComment();
        BeanUtil.copyProperties(commentDto,orderComment);
        //查询商品
        ShopGoodsSku goodSku = goodsSkuService.getById(commentDto.getGoodSkuId());
        ShopGoodsInfo goodsInfo = goodsInfoService.getById(goodSku.getShopGoodsId());

        orderComment.setType("1");
        orderComment.setGoodsId(goodsInfo.getId().toString());
        orderComment.setGoodsName(goodsInfo.getGoodsName());
        orderComment.setSkuName(goodSku.getSkuName());
        orderComment.setEnable(commentDto.getEnable());
        orderComment.setDeleted(false);
        orderComment.setCreatedTime(commentDto.getCreatedTime());
        orderComment.setUpdatedTime(commentDto.getCreatedTime());
        return commentService.save(orderComment)?R.ok():R.fail();
    }
    @Operation(summary = "查看商品")
    @PostMapping("/pageGoods")
    public R<Page<ShopProductDto>> pageGoods(@RequestBody QueryGoodsComment data){
        Page<ShopGoodsInfo> page = Page.of(data.getPageNum(), data.getPageSize());
        LambdaQueryWrapper<ShopGoodsInfo> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtils.isNotEmpty(data.getShopId())){
            queryWrapper.eq(ShopGoodsInfo::getShopId,data.getShopId());
        }
        if (ObjectUtils.isNotEmpty(data.getName())){
            queryWrapper.like(ShopGoodsInfo::getGoodsName,data.getName());
        }
        queryWrapper.ne(ShopGoodsInfo::getIsDelete,"1");
        Page<ShopGoodsInfo> infoPage = goodsInfoService.page(page, queryWrapper);
        Page<ShopProductDto> productPage = new Page<>();
        BeanUtil.copyProperties(infoPage,productPage);
        productPage.setRecords(new ArrayList<ShopProductDto>());
        if (ObjectUtils.isEmpty(infoPage.getRecords()))return R.ok(productPage);
        for (ShopGoodsInfo record : infoPage.getRecords()) {
            ShopProductDto product = new ShopProductDto();
            BeanUtil.copyProperties(record,product);
            LambdaQueryWrapper<ShopGoodsSku> skuLambdaQueryWrapper = new LambdaQueryWrapper<>();
            skuLambdaQueryWrapper.eq(ShopGoodsSku::getShopGoodsId,record.getId());
            List<ShopGoodsSku> sku = goodsSkuService.list(skuLambdaQueryWrapper);
            product.setGoodsSkus(sku);
            productPage.getRecords().add(product);
        }
        return R.ok(productPage);
    }

    @Operation(summary = "回复评论")
    @PostMapping("/add")
    @CheckWord
    public R<?> add(@RequestBody OrderCommentSysDto commentDto){
        long now = new Date().getTime();
        ShopOrderComment orderComment = new ShopOrderComment();
        orderComment.setParentId(commentDto.getParentId());
        orderComment.setContent(commentDto.getContent());
        orderComment.setType("2");
        orderComment.setDeleted(false);
        orderComment.setCreatedTime(now);
        orderComment.setUpdatedTime(now);
        return commentService.save(orderComment)?R.ok():R.fail();
    }

    @Operation(summary = "评论列表")
    @PostMapping("/page")
    public R<Page<OrderCommentVo>> page(@RequestBody QueryOrderComment queryOrderComment){
        Page<ShopOrderComment> page = Page.of(queryOrderComment.getCurrent(), queryOrderComment.getSize());
        LambdaQueryWrapper<ShopOrderComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopOrderComment::getParentId,"0");
        queryWrapper.eq(ShopOrderComment::getDeleted,false);
        queryWrapper.orderByDesc(ShopOrderComment::getCreatedTime);
        Page<ShopOrderComment> commentList = commentService.page(page, queryWrapper);
        Page<OrderCommentVo> pageTree = new Page<>();
        BeanUtil.copyProperties(commentList,pageTree);
        pageTree.setRecords(new ArrayList<OrderCommentVo>());
        for (ShopOrderComment record : commentList.getRecords()) {
            OrderCommentVo commentVo = new OrderCommentVo();
            BeanUtil.copyProperties(record,commentVo);
            LambdaQueryWrapper<ShopOrderComment> queryWrapperChild = new LambdaQueryWrapper<>();
            queryWrapperChild.eq(ShopOrderComment::getParentId,record.getId());
            List<ShopOrderComment> commentChild = commentService.list(queryWrapperChild);
            commentVo.setChildOrderComent(commentChild);
            pageTree.getRecords().add(commentVo);
        }
        return R.ok(pageTree);
    }
    @Operation(summary = "隐藏评论")
    @GetMapping("/{id}")
    public R enable(@PathVariable Long id){
        ShopOrderComment comment = commentService.getById(id);
        if (ObjectUtils.isEmpty(comment))return R.fail(MessageUtils.message("comment.does.not.exist"));
        comment.setEnable(comment.getEnable().equals("0")?"1":"0");
       return commentService.updateById(comment)? R.ok():R.fail();
    }
    @Operation(summary = "删除评论")
    @DeleteMapping("/{id}")
    public R del(@PathVariable Long id){
        ShopOrderComment comment = commentService.getById(id);
        if (ObjectUtils.isEmpty(comment))return R.fail(MessageUtils.message("comment.does.not.exist"));
        comment.setDeleted(true);
       return commentService.updateById(comment)? R.ok():R.fail();
    }
}
